﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AutoMapper;
using EFEntity;
using Iservices;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using Model;

namespace Services
{
  public  class major_changeServices:Imajor_changeServices
    {
        private readonly MyDbContext myDbContext;

        public major_changeServices(MyDbContext myDbContext)
        {
            this.myDbContext = myDbContext;

        }
		//分页
		public async Task<List<major_change>> major_changeFenYe(FenYe fy)
		{
			SqlParameter[] ps = {
			new SqlParameter(){  ParameterName="@pageSize",Value=fy.PageSize},
			new SqlParameter(){  ParameterName="@keyName",Value="mch_id"},
			new SqlParameter(){  ParameterName="@tabelName",Value="major_change"},
			new SqlParameter(){  ParameterName="@where",Value=fy.Where},
			new SqlParameter(){  ParameterName="@currentPage",Value=fy.CurrentPage},	
			new SqlParameter(){  ParameterName="@rows",Direction=ParameterDirection.Output,SqlDbType= SqlDbType.Int},
			new SqlParameter(){  ParameterName="@pages",Direction= ParameterDirection.Output,SqlDbType= SqlDbType.Int},
			};
			List<Emajor_change> list = await myDbContext.major_change.FromSqlRaw("exec [dbo].[proc_Fenye] @pageSize, @keyName, @tabelName, @where, @currentPage,@rows out, @pages out", ps).ToListAsync();
			fy.Rows = (int)ps[5].Value;
			fy.Pages = (int)ps[6].Value;
			//把EO-DTO
			List<major_change> list2 = new List<major_change>();
			foreach (Emajor_change item in list)
			{
				major_change t = new major_change()
				{
					mch_id = item.mch_id,
					first_kind_id = item.first_kind_id,
					first_kind_name = item.first_kind_name,
					second_kind_id = item.second_kind_id,
					second_kind_name = item.second_kind_name,
					third_kind_id = item.third_kind_id,
					third_kind_name = item.third_kind_name,
					major_kind_id = item.major_kind_id,
					major_kind_name = item.major_kind_name,
					major_id = item.major_id,
					major_name = item.major_name,
					new_first_kind_id = item.new_first_kind_id,
					new_first_kind_name = item.new_first_kind_name,
					new_second_kind_id = item.new_second_kind_id,
					new_second_kind_name = item.new_second_kind_name,
					new_third_kind_id = item.new_third_kind_id,
					new_third_kind_name = item.new_third_kind_name,
					new_major_kind_id = item.new_major_kind_id,
					new_major_kind_name = item.new_major_kind_name,
					new_major_id = item.new_major_id,
					new_major_name = item.new_major_name,
					human_id = item.human_id,
					human_name = item.human_name,
					salary_standard_id = item.salary_standard_id,
					salary_standard_name = item.salary_standard_name,
					salary_sum = item.salary_sum,
					new_salary_standard_id = item.new_salary_standard_id,
					new_salary_standard_name = item.new_salary_standard_name,
					new_salary_sum = item.new_salary_sum,
					change_reason = item.change_reason,
					check_reason = item.check_reason,
					check_status = item.check_status,
					register = item.register,
					checker = item.checker,
					regist_time = item.regist_time,
					check_time = item.check_time,
				};
				list2.Add(t);
			}
			return list2;
		}
		//分页
		public async Task<List<major_change>> major_changeFenYe2(FenYe fy)
        {
			var data = myDbContext.major_change.OrderBy(e => e.mch_id).Where(e=>e.check_status==1).ToList();
			fy.Rows = data.Count();
			fy.Pages = (fy.Rows - 1) / fy.PageSize + 1;
			List<Emajor_change> list = new List<Emajor_change>();
			await Task.Run(() =>
			{
				list = data.Skip((fy.CurrentPage - 1) * fy.PageSize)
					.Take(fy.PageSize)
					.ToList();
			});
			//把EO-DTO
			List<major_change> list2 = new List<major_change>();
			foreach (Emajor_change item in list)
			{
				major_change t = new major_change()
				{
					mch_id = item.mch_id,
					first_kind_id = item.first_kind_id,
					first_kind_name = item.first_kind_name,
					second_kind_id = item.second_kind_id,
					second_kind_name = item.second_kind_name,
					third_kind_id = item.third_kind_id,
					third_kind_name = item.third_kind_name,
					major_kind_id = item.major_kind_id,
					major_kind_name = item.major_kind_name,
					major_id = item.major_id,
					major_name = item.major_name,
					new_first_kind_id = item.new_first_kind_id,
					new_first_kind_name = item.new_first_kind_name,
					new_second_kind_id = item.new_second_kind_id,
					new_second_kind_name = item.new_second_kind_name,
					new_third_kind_id = item.new_third_kind_id,
					new_third_kind_name = item.new_third_kind_name,
					new_major_kind_id = item.new_major_kind_id,
					new_major_kind_name = item.new_major_kind_name,
					new_major_id = item.new_major_id,
					new_major_name = item.new_major_name,
					human_id = item.human_id,
					human_name = item.human_name,
					salary_standard_id = item.salary_standard_id,
					salary_standard_name = item.salary_standard_name,
					salary_sum = item.salary_sum,
					new_salary_standard_id = item.new_salary_standard_id,
					new_salary_standard_name = item.new_salary_standard_name,
					new_salary_sum = item.new_salary_sum,
					change_reason = item.change_reason,
					check_reason = item.check_reason,
					check_status = item.check_status,
					register = item.register,
					checker = item.checker,
					regist_time = item.regist_time,
					check_time = item.check_time,
				};
				list2.Add(t);
			}
			return list2;
		}

		//查看全部
		public async Task<List<major_change>> major_changeselectAll()
        {
            List<Emajor_change> list = new List<Emajor_change>();
            await Task.Run(() =>
            {
                list = myDbContext.major_change.ToList();
            });
            List<major_change> list2 = new List<major_change>();
            foreach (Emajor_change item in list)
            {
                major_change fd = new major_change()
                {
					mch_id = item.mch_id,
					first_kind_id = item.first_kind_id,
					first_kind_name = item.first_kind_name,
					second_kind_id = item.second_kind_id,
					second_kind_name = item.second_kind_name,
					third_kind_id = item.third_kind_id,
					third_kind_name = item.third_kind_name,
					major_kind_id = item.major_kind_id,
					major_kind_name = item.major_kind_name,
					major_id = item.major_id,
					major_name = item.major_name,
					new_first_kind_id = item.new_first_kind_id,
					new_first_kind_name = item.new_first_kind_name,
					new_second_kind_id = item.new_second_kind_id,
					new_second_kind_name = item.new_second_kind_name,
					new_third_kind_id = item.new_third_kind_id,
					new_third_kind_name = item.new_third_kind_name,
					new_major_kind_id = item.new_major_kind_id,
					new_major_kind_name = item.new_major_kind_name,
					new_major_id = item.new_major_id,
					new_major_name = item.new_major_name,
					human_id = item.human_id,
					human_name = item.human_name,
					salary_standard_id = item.salary_standard_id,
					salary_standard_name = item.salary_standard_name,
					salary_sum = item.salary_sum,
					new_salary_standard_id = item.new_salary_standard_id,
					new_salary_standard_name = item.new_salary_standard_name,
					new_salary_sum = item.new_salary_sum,
					change_reason = item.change_reason,
					check_reason = item.check_reason,
					check_status = item.check_status,
					register = item.register,
					checker = item.checker,
					regist_time = item.regist_time,
					check_time = item.check_time,
				};
                list2.Add(fd);
            }
            return list2;
        }
	
		//根据id查询信息
		public async Task<major_change> GetSelId(int id)
		{
			Emajor_change ss = new Emajor_change();
			await Task.Run(() =>
			{
				ss = myDbContext.major_change.Where(e => e.mch_id == id).FirstOrDefault();
			});
			major_change s = new major_change()
			{
				mch_id = ss.mch_id,
				first_kind_id = ss.first_kind_id,
				first_kind_name = ss.first_kind_name,
				second_kind_id = ss.second_kind_id,
				second_kind_name = ss.second_kind_name,
				third_kind_id = ss.third_kind_id,
				third_kind_name = ss.third_kind_name,
				major_kind_id = ss.major_kind_id,
				major_kind_name = ss.major_kind_name,
				major_id = ss.major_id,
				major_name = ss.major_name,
				new_first_kind_id = ss.new_first_kind_id,
				new_first_kind_name = ss.new_first_kind_name,
				new_second_kind_id = ss.new_second_kind_id,
				new_second_kind_name = ss.new_second_kind_name,
				new_third_kind_id = ss.new_third_kind_id,
				new_third_kind_name = ss.new_third_kind_name,
				new_major_kind_id = ss.new_major_kind_id,
				new_major_kind_name = ss.new_major_kind_name,
				new_major_id = ss.new_major_id,
				new_major_name = ss.new_major_name,
				human_id = ss.human_id,
				human_name = ss.human_name,
				salary_standard_id = ss.salary_standard_id,
				salary_standard_name = ss.salary_standard_name,
				salary_sum = ss.salary_sum,
				new_salary_standard_id = ss.new_salary_standard_id,
				new_salary_standard_name = ss.new_salary_standard_name,
				new_salary_sum = ss.new_salary_sum,
				change_reason = ss.change_reason,
				check_reason = ss.check_reason,
				check_status = ss.check_status,
				register = ss.register,
				checker = ss.checker,
				regist_time = ss.regist_time,
				check_time = ss.check_time,
			};
			return s;
		}
		public async Task<int> Upd(major_change t) //修改
		{
			int num = 0;
			Emajor_change te = new Emajor_change()
			{
				salary_standard_id = t.salary_standard_id,
				salary_standard_name = t.salary_standard_name,
				salary_sum = t.salary_sum,
				second_kind_id = t.second_kind_id,
				second_kind_name = t.second_kind_name,
				check_status = t.check_status,
				change_reason = t.change_reason,
				checker = t.checker,
				check_reason = t.check_reason,
				check_time = t.check_time,
				first_kind_id = t.first_kind_id,
				first_kind_name = t.first_kind_name,
				new_first_kind_id = t.first_kind_name,
				new_first_kind_name = t.new_first_kind_name,
				new_major_id = t.new_major_id,
				new_major_kind_id = t.new_major_kind_id,
				new_major_kind_name = t.new_major_kind_name,
				new_major_name = t.new_major_name,
				new_salary_standard_id = t.new_salary_standard_id,
				new_salary_standard_name = t.new_salary_standard_name,
				new_salary_sum = t.new_salary_sum,
				new_second_kind_id = t.new_second_kind_id,
				new_second_kind_name = t.new_second_kind_name,
				new_third_kind_id = t.new_third_kind_id,
				new_third_kind_name = t.new_third_kind_name,
				human_name = t.human_name,
				human_id = t.human_id,
				major_id = t.major_id,
				major_kind_id = t.major_kind_id,
				major_kind_name = t.major_kind_name,
				major_name = t.major_name,
				mch_id = t.mch_id,
				register = t.register,
				third_kind_id = t.third_kind_id,
				third_kind_name = t.third_kind_name,
				regist_time = t.regist_time,
			};
			//设置实体状态
			myDbContext.Entry(te).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
			num = await myDbContext.SaveChangesAsync();//使用异步修改实体其中的值
			return num;
		}
	}
}
